# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pdal
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.4.3
pkgrel=4
pkgdesc="A C++ library for translating and manipulating point cloud data (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="http://www.pdal.io"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gdal")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-hdf5")
            #"${MINGW_PACKAGE_PREFIX}-tiledb")
optdepends=("${MINGW_PACKAGE_PREFIX}-hdf5: HDF plugin, read data in the HDF format"
            "${MINGW_PACKAGE_PREFIX}-tiledb: TileDB plugin, read/write data from TileDB")
source=("https://github.com/PDAL/PDAL/releases/download/${pkgver}/PDAL-${pkgver}-src.tar.gz"
        "0001-fileutils.patch"
        "0002-remove-msvc-link-options.patch"
        "0003-use-wmain-only-with-msvc.patch")
sha256sums=('abac604c6dcafdcd8a36a7d00982be966f7da00c37d89db2785637643e963e4c'
            '465744dde252f15dd6f97901d007d64bf7e70b34354efcf500677973974e7f1d'
            'ae09ed935887b4b04a529c2c6fa3bc6dc7280f8dbafb221ff2991ed6cfd36fcf'
            '4c0bd2eed24ba53808f3ef1e454182b3e6aff7cf9b108857ade96c2eca51db9b')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/PDAL-${pkgver}-src"

  apply_patch_with_msg \
    0001-fileutils.patch \
    0002-remove-msvc-link-options.patch \
    0003-use-wmain-only-with-msvc.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -Wno-dev \
    -GNinja \
    ${extra_config[@]} \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_SHARED_LIBS=ON \
    -DWITH_TESTS=OFF \
    -DBUILD_PLUGIN_HDF=ON \
    -DBUILD_PLUGIN_TILEDB=OFF \
    ../PDAL-${pkgver}-src

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/PDAL-${pkgver}-src/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
